package com.example.onlinemusic.controller;

import com.example.onlinemusic.config.AppConfig;
import com.example.onlinemusic.mapper.UserMapper;
import com.example.onlinemusic.model.Contant;
import com.example.onlinemusic.model.User;
import com.example.onlinemusic.tools.ResponseBodyMessage;
import org.apache.ibatis.annotations.Param;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: hold on
 * Date: 2022-07-26
 * Time: 15:12
 */
@RestController // @ResponseBody + @Controller
@RequestMapping("/user") //设置路由 用来映射请求!
public class UserController {
    //将UserMapper注入!
    @Resource
    private UserMapper userMapper;

    @Resource
    private AppConfig appConfig;


    //注册功能
    @RequestMapping("/register")
    public ResponseBodyMessage<User>register(@RequestParam String username, @RequestParam String password, HttpServletRequest request, HttpServletResponse response){
        //1.首先查询该用户是否存在!
        User user = userMapper.selectUserByUserName(username);
        if(user!=null){//查询到该用户,说明用户存在!
            return new ResponseBodyMessage<User>(-1,"该用户已注册,请修改用户名重新注册",null);
        }
        //2.用户不存在,就注册该用户!
        //对密码进行加密后保存在数据库中!
        password = appConfig.getBCryptPasswordEncoder().encode(password);
        //将用户注册到数据库中!
        userMapper.register(username,password);
        //将注册好的用户信息放回给客户端!
        user = userMapper.selectUserByUserName(username);
        request.getSession().setAttribute(Contant.USERINFO_SESSION_KEY,user);
        return new ResponseBodyMessage<User>(0,"注册成功!",user);
    }

    //登入功能
//未进行密码加密!
//    @RequestMapping("/login")
//    //@RequestParam SpringMVC下的注解,表示该参数必须传入给服务器!
//    //value = "前端参数名",required = true/false,defaultValue ="默认值"
//    //这里required设置为ture表示该参数必传,默认为true,如果设置了defaultValue那默认required为false
//    public ResponseBodyMessage<User> login(@RequestParam String username, @RequestParam String password, HttpServletRequest request){
//        User userLogin = new User();
//        userLogin.setUsername(username);
//        userLogin.setPassword(password);
//        //调用mapper下的 login查询user!
//        User user = userMapper.login(userLogin);
//        //返回响应
//        if(user!=null){//登入成功!
//            //登入成功就在服务器保存该Session会话!
//            //这里我们的key值可以通过常量值设置,避免后面出错!
//            //request.getSession().setAttribute("USERINFO_SESSION_KEY",user);
//           // request.getSession().setAttribute(Contant.USERINFO_SESSION_KEY,user);
//          return new  ResponseBodyMessage<User> (0,"登入成功",userLogin);
//        }else{
//          return new  ResponseBodyMessage<User> (-1,"登入失败",userLogin);
//        }
//    }

//通过BCrypet加密!
//因为我们matches通过前端传输过来的密码对比数据库中密码即可判断密码是否正确!
//我们知道每次encode后的密码都不一样,所以我们不能通过查询数据库中username+password验证!
//我们先通过username查询到数据库中的密码,然后通过matches匹配判断是否登入成功!

    @RequestMapping("/login")
    public ResponseBodyMessage<User> login(@RequestParam String username, @RequestParam String password, HttpServletRequest request){
        User user = userMapper.selectUserByUserName(username);
        //返回响应
        if(user!=null){//查询到username用户!
                 //匹配验证密码是否正确!
            boolean flg = appConfig.getBCryptPasswordEncoder().matches(password,user.getPassword());
            if(flg){//登入成功!
                request.getSession().setAttribute(Contant.USERINFO_SESSION_KEY,user);
                System.out.println("session:"+request.getSession().getAttribute(Contant.USERINFO_SESSION_KEY));
                return new  ResponseBodyMessage<User> (0,"登入成功",user);
            }else{//密码错误!
                return new  ResponseBodyMessage<User> (-1,"用户名或密码错误",user);
            }

        }else{//用户不存在!
          return new  ResponseBodyMessage<User> (-1,"用户名或密码错误",user);
        }
    }

}
